package com.jy.app.lib.common;

import android.app.Activity;
import android.content.Intent;
import android.support.v4.app.Fragment;

import java.util.HashMap;
import java.util.Map;

public class CommonFragment extends Fragment {

    private Map<Integer,StartActivityResult> startPageResultMap = new HashMap<>();

    public void startActivityForResult(Intent intent, int requestCode, StartActivityResult result){
        startPageResultMap.put(requestCode,result);
        startActivityForResult(intent,requestCode);
    }



    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (startPageResultMap.size() != 0){
            StartActivityResult pageResult = startPageResultMap.get(requestCode);
            if (pageResult!=null){
                if (pageResult instanceof StartActivityOkResult){
                    if (resultCode == Activity.RESULT_OK){
                        pageResult.onResult(requestCode,resultCode,data);
                    }
                }else{
                    pageResult.onResult(requestCode,resultCode,data);
                }
                startPageResultMap.remove(requestCode);
            }
        }
        super.onActivityResult(requestCode, resultCode, data);
    }
}
